﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace MyFramework
{
    /// <summary>
    /// UI层级
    /// </summary>
    public enum UILayer
    {
        Bg,
        Common,
        Top
    }
    public class GUIManager:MonoBehaviour
    {
        private static GameObject mUIRootPrefab;
        /// <summary>
        /// 获取UIRoot
        /// </summary>
        public static GameObject UIRoot
        {
            get
            {
                if (mUIRootPrefab==null)
                {
                    var uirootprefab = Resources.Load<GameObject>("UIRoot");
                    mUIRootPrefab = GameObject.Instantiate(uirootprefab);
                    mUIRootPrefab.name = "UIRoot";
                }
                return mUIRootPrefab;
            }
        }
        private static Dictionary<string, GameObject> mUIPanelDic = new Dictionary<string, GameObject>();
        /// <summary>
        /// 卸载UIPanel
        /// </summary>
        /// <param name="UIName"></param>
        public static void UnLoadPanel(string UIName)
        {
            Debug.Log("dhu");
            if (mUIPanelDic.ContainsKey(UIName))
            {
                Destroy(mUIPanelDic[UIName]);
            }
        }

        public static GameObject LoadPanel(string UIName, UILayer layer)
        {
            var uiPanelPrefab = Resources.Load<GameObject>(UIName);
            var panel = GameObject.Instantiate(uiPanelPrefab);
            panel.name = UIName;

            mUIPanelDic.Add(UIName,panel);

            switch (layer)
            {
                case UILayer.Bg:
                    panel.transform.SetParent(UIRoot.transform.Find("Bg"));
                    break;
                case UILayer.Common:
                    panel.transform.SetParent(UIRoot.transform.Find("Common"));
                    break;
                case UILayer.Top:
                    panel.transform.SetParent(UIRoot.transform.Find("Top"));
                    break;
                default:
                    break;
            }

            var panelRectTrans = panel.GetComponent<RectTransform>();

            panelRectTrans.offsetMin = Vector2.zero;
            panelRectTrans.offsetMax = Vector2.zero;
            panelRectTrans.anchoredPosition3D = Vector3.zero;
            panelRectTrans.anchorMin = Vector2.zero;
            panelRectTrans.anchorMax = Vector2.one;

            return panel;
        }
        /// <summary>
        /// UI适配屏幕
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="matchWidthOrHeight"></param>
        public static void SetResolution(float width,float height,float matchWidthOrHeight)
        {
            var UIRootScaler = UIRoot.GetComponent<CanvasScaler>();
            UIRootScaler.referenceResolution = new Vector2(width,height);
            UIRootScaler.matchWidthOrHeight = matchWidthOrHeight;
        }
    }
}
